#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    system_init();//系统初始化
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::system_init()
{   //n,8,1
    global_port.setParity(QSerialPort::NoParity);
    global_port.setDataBits(QSerialPort::Data8);
    global_port.setStopBits(QSerialPort::OneStop);

    //链接槽函数
    connect(ui->btn_JianCeChuanKou,&QPushButton::clicked,this,&::MainWindow::but_JianCeComm);
    connect(ui->but_Open,&QPushButton::clicked,this,&::MainWindow::btn_open_port);
    connect(ui->but_send,&QPushButton::clicked,this,&::MainWindow::btn_send_data);
    connect(&global_port,&QSerialPort::readyRead,this,&::MainWindow::receive_data);
    connect(ui->but_close,&QPushButton::clicked,this,&::MainWindow::btn_close_port);
    connect(ui->btn_qingkong,&QPushButton::clicked,this,&::MainWindow::btn_QingKongJieShou);

}

void MainWindow::but_JianCeComm(bool)//检测系统内串口信息
{
    ui->com_Port_no->clear();//清空当前列表
    //检索端口
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->com_Port_no->addItem(info.portName());
    }

}

void MainWindow::btn_close_port(bool)
{
    global_port.close();
    ui->label->setText("串口未打开");
}

void MainWindow::btn_open_port(bool)
{
    qDebug()<<ui->com_Port_no->currentIndex();
    /*
    switch (ui->com_Port_no->currentIndex()) {
    case 0:
        global_port.setPortName("COM1");
        break;
    case 1:
        global_port.setPortName("COM2");
        break;
    default:
        global_port.setPortName("COM3");
        break;
    }
    */
    global_port.setPortName(ui->com_Port_no->currentText());//设置端口号
    qDebug()<<ui->com_Port_no->currentText();
    ///////////////////////////////
    /*
    switch (ui->com_bot->currentIndex()) {
    case 0:
        global_port.setBaudRate(QSerialPort::Baud115200);
        break;
    default:
        global_port.setBaudRate(QSerialPort::Baud9600);
        break;
    }
    */
    global_port.setBaudRate(ui->com_bot->currentText().toInt());//设置波特率
    qDebug()<<ui->com_bot->currentText().toInt();
    ////////////////////////////////////////
    global_port.open(QIODevice::ReadWrite);//读写模式打开串口
    ui->label->setText("串口已打开");

}
void MainWindow::btn_send_data(bool)
{
    QString send_data = ui->lineEdit->text();//从LineEdit得到字符串
    QByteArray senddata;

    if(ui->checkBox->checkState() == Qt::Checked)
    {
        String2Hex(send_data,senddata);//将str字符串转换为16进制的形式
        global_port.write(senddata);//16进制发送数据
    }else{

        //文本模式发送数据
        QByteArray send_array = send_data.toLatin1();//QString类型转换成 QByteArray
        global_port.write(send_array);//串口发送数据

    }

}
void MainWindow::receive_data()
{
    QByteArray array = global_port.readAll();
    if(ui->checkBox->checkState() == Qt::Checked)
    {
        ui->global_port->insertPlainText(QString(array.toHex(' ').toUpper().append(' ')));//16进制显示
    }else{
        ui->global_port->insertPlainText(QString(array));//文本模式
    }
}
void MainWindow::btn_QingKongJieShou()
{
    ui->global_port->setPlainText("");


}

void MainWindow::String2Hex(QString str, QByteArray &senddata)
{
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    char lstr,hstr;
    for(int i=0; i<len; )
    {
        //char lstr,
        //hstr=str[i].toAscii();//qt4

        hstr=str[i].toLatin1();//qt5
        if(hstr == ' ')
        {
            i++;
            continue;
        }
        i++;
        if(i >= len)
            break;
        //lstr = str[i].toAscii();//qt4
        lstr = str[i].toLatin1();//qt5
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
            break;
        else
            hexdata = hexdata*16+lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
}
char MainWindow::ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9'))
        return ch-0x30;
    else if((ch >= 'A') && (ch <= 'F'))
        return ch-'A'+10;
    else if((ch >= 'a') && (ch <= 'f'))
        return ch-'a'+10;
    else return (-1);
}


